package com.sdgakj.utils;

import java.util.Random;

public class CdkeyUtil {

    public static final String LETTERANDNUMBER = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
    public static final String LETTERANDNUMBER_NOIO = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghgklmnpqrstuvwxyz0123456789";
    public static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String LOWERCASE = "abcdefghigklmnopqrstuvwxyz";
    public static final String NUMBER = "0123456789";

    public static String generateCdkey(int len, String generateString) {
        char[] ch = generateString.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append(ch[new Random().nextInt(ch.length)]);
        }
        return sb.toString();
    }

    public static String generateCdKeyWithLine(int len, String generateString) {
        char[] ch = generateString.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= len; i++) {
            sb.append(ch[new Random().nextInt(ch.length)]);
            if (i % 4 == 0) {
                sb.append("-");
            }
        }
        return sb.substring(0, sb.toString().length() - 1);
    }

    /**
     * 生成指定前缀或者后缀的cdkey串
     *
     * @param pre          设定cdkey的前缀
     * @param suffix       设定cdkey的后缀
     * @param optionString 设定cdkey的可选项
     * @param len          cdkey的长度
     * @return
     */
    public static String build(String pre, String suffix, String optionString, int len) {
        if (pre.length() > len || suffix.length() > len) {
            return "前缀或后缀的长度大于cdkey的指定长度len";
        }
        len = len - pre.length() - suffix.length();
        return pre + generateCdkey(len, optionString) + suffix;
    }

    public static void main(String[] args) {
//        String generateString = LETTERANDNUMBER;
//        System.out.println(build("AAA", "MM", generateString, 16));
    }
}